package es.xpt.ysf.commons.changelogger;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import es.xpt.ysf.commons.business.model.CurrencyExchange;
import es.xpt.ysf.commons.changelogger.model.Change;
import es.xpt.ysf.commons.changelogger.model.ChangeDetail;
import es.xpt.ysf.dao.Connection;
import es.xpt.ysf.dao.DAOErrors;
import es.xpt.ysf.dao.DAOException;
import es.xpt.ysf.dao.DbDAO;
import es.xpt.ysf.dao.PreparedStatement;
import es.xpt.ysf.jaas.YSFPrincipal;

/**
 * Change logger class for register changes only
 *
 */
public class ChangeLoggerDAO extends DbDAO<CurrencyExchange>  {
	
	/**
	 * @param c
	 * @param principal
	 * @param errors
	 */
	public ChangeLoggerDAO(Connection c, YSFPrincipal principal, DAOErrors errors) {
		super(c, principal, errors);
	}
	
	/**
	 * Insert logger booking changes 
	 * @param list
	 * @throws DAOException
	 */
	public void insertChanges(List<Change> changeList, String logTable) throws DAOException {

		if(changeList==null || changeList.isEmpty())
			return ;
		
	    PreparedStatement stmt = null;
	    PreparedStatement stmtDetail = null;
	    
	    try {
			stmt = con.prepareStatement("INSERT INTO " + logTable + "_change (id, executor, timeStamp, externalReference) VALUES (?,?,?,?)");
			stmtDetail = con.prepareStatement("INSERT INTO " + logTable + "_changeDetail (id, keyName, value) VALUES (?,?,?)");
			
			for (Change change : changeList) {
				stmt.setString(1, change.getCode());
				stmt.setString(2, change.getExecutor());
				stmt.setTimestamp(3, new Timestamp(change.getDate().getTime()));
				stmt.setString(4, change.getExternalReference());

				if (changeList.size()>1)
					stmt.addBatch();
				
				if(change.getChanges()!=null && !change.getChanges().isEmpty())
					addChangesDetail(stmtDetail,change.getCode(),change.getChanges());
			}
			
			if (changeList.size()>1)
				stmt.executeBatch();
			else
				stmt.executeUpdate();
			// insert details
			stmtDetail.executeBatch();
      
	    } catch (SQLException se){
	    	throw new DAOException(se, errors);
	    } finally {
	    	if (stmt != null) stmt.close();
	    	if (stmtDetail != null) stmtDetail.close();
	    }		
	}
	
	/**
	 * Add logger detail values 
	 * @param stmtDetail 
	 * @param parentId
	 * @param changeDetailList
	 * @throws DAOException
	 */
	public void addChangesDetail(PreparedStatement stmtDetail, String parentId, List<ChangeDetail> changeDetailList) throws DAOException {
			
	    try {
			for (ChangeDetail changeDetail : changeDetailList) {
				stmtDetail.setString(1, parentId);
				stmtDetail.setString(2, changeDetail.getKey());
				stmtDetail.setString(3, String.valueOf(changeDetail.getValue()));
				
				stmtDetail.addBatch();
			}
      
	    } catch (SQLException se){
	      throw new DAOException(se, errors);
	    }	
	}

	@Override
	protected CurrencyExchange populateBean(ResultSet rs) throws SQLException {
		return null;
	}
}